package com.itkaien.anno.datasourceDemomybatis;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Configuration
@Component
//@Import() 可以导入三种类型的配置文件 1.配置类 2.配置文件 3.第三方的配置文件
@ComponentScan(basePackages = "com.itkaien.anno.datasourceDemomybatis")
@PropertySource(value = "classpath:JDBC.properties")
@MapperScan(basePackages = "com.itkaien.springzh.Mybatis.mapper")
@Import(MyImportSelector.class)
public class connectiontUtils {

    @Bean
    public DataSource dataSource(@Value("${JDBC.driverClassName}") String driverClassName,
                                    @Value("${JDBC.url}") String url,
                                    @Value("${JDBC.username}") String username,
                                    @Value("${JDBC.password}") String password
                                    ){

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public SqlSessionFactoryBean SqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        // 设置 MyBatis 配置属性
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true); // 开启驼峰映射

        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean;
    }



}
